package com.zyd.service.city.impl;

import com.google.common.collect.Lists;
import com.zyd.context.HeaderContext;
import com.zyd.io.model.*;
import com.zyd.io.vo.city.CityVO;
import com.zyd.io.vo.city.CountryVO;
import com.zyd.io.vo.city.ProvinceVO;
import com.zyd.mappers.CityEntityMapper;
import com.zyd.mappers.CountyEntityMapper;
import com.zyd.mappers.ProvinceEntityMapper;
import com.zyd.service.city.CityService;
import com.zyd.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.jdbc.core.RowMapper;
import java.util.Arrays;
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;

@Component
public class CityServiceImpl implements CityService {

    @Autowired
    private ProvinceEntityMapper provinceEntityMapper;

    @Autowired
    private CityEntityMapper cityEntityMapper;


    @Autowired
    private CountyEntityMapper countyEntityMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<ProvinceVO> findCitys( ) {
        List<ProvinceVO> ret = Lists.newArrayList();
        ProvinceEntityExample provinceEntityExample = new ProvinceEntityExample();
        List<ProvinceEntity> provinces = provinceEntityMapper.selectByExample(provinceEntityExample);
        //方法获取userid
        String userid=(String) HeaderContext.get().get("userid");
		System.out.println("输出userid"+userid);
        String sql ="";
		if(userid == null)
		{
			sql = "select area from zydzn_power where roleid='1' and menuid='2010'";
		
		}
        else if("null".equals(userid))
        {
            sql = "select area from zydzn_power where roleid='1' and menuid='2010'";
        }
        else
        {
            sql = "select area from zydzn_power where roleid=(select usertype from zydzn_users where id= '"+userid+"' ) and menuid='2010'";
		
        }
        //获取uid 所在区域的权限
         
		 System.out.println(sql);
         List<String> areas = jdbcTemplate.query(sql, new RowMapper<String>() {
            @Override
            public String mapRow(ResultSet rs, int rowNum) throws SQLException {  
				return  rs.getString("area");
            }
        });
		String area=areas.get(0);
		
        String[] split = area.split(",");
		System.out.println("输出area"+area);
        for (ProvinceEntity province : provinces) {
            ProvinceVO provinceVO = BeanUtils.transfrom(ProvinceVO.class, province);
            List<CityVO> cityVOs = Lists.newArrayList();
            CityEntityExample cityEntityExample = new CityEntityExample();
            cityEntityExample.or().andProvincecodeEqualTo(province.getCode());
            List<CityEntity> citys = cityEntityMapper.selectByExample(cityEntityExample);
            for (CityEntity city : citys) {
                CityVO cityVO = BeanUtils.transfrom(CityVO.class, city);
                List<CountryVO> countryVOS = Lists.newArrayList();
                CountyEntityExample countyEntityExample = new CountyEntityExample();
                countyEntityExample.or().andCitycodeEqualTo(city.getCode());
                List<CountyEntity> countys = countyEntityMapper.selectByExample(countyEntityExample);
                for (CountyEntity county : countys) {
                    CountryVO countryVO = BeanUtils.transfrom(CountryVO.class, county);
                    for (String s : split) {
						System.out.println("输出权限区县"+s);
						System.out.println("输出比较区县"+countryVO.getId());
                        if (countryVO.getId().equals(s)) {
                            countryVOS.add(countryVO);
                        }
                    }
                }
                cityVO.setCountrys(countryVOS);
                if (!countryVOS.isEmpty()) {
                    cityVOs.add(cityVO);
                }
            }
			 provinceVO.setCitys(cityVOs);
			 if (!cityVOs.isEmpty()) {
                   ret.add(provinceVO);
                }
        }
        return ret;
    }


}
